package com.homelock.controller;

import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.homelock.service.UserCommunityService;
import com.pi.base.dto.result.AppResult;
import com.pi.http.base.controller.BaseController;

@RequestMapping("/user/community")
@RestController
public class UserCommunityController extends BaseController {

    private UserCommunityService userCommunityService;

    public UserCommunityController(UserCommunityService userCommunityService) {
        Assert.notNull(userCommunityService, "小区服务为空");
        this.userCommunityService = userCommunityService;
    }

    @RequestMapping("/search")
    public AppResult search(String name) {
        return AppResult.newSuccessResult(userCommunityService.queryByName(name));
    }

    @RequestMapping("/bind")
    public AppResult bind(Long devId, Long communityId) {
        userCommunityService.bindCommunity(getLoginUserId(), devId, communityId);
        return AppResult.OK;
    }

    @RequestMapping("/unbind")
    public AppResult unbind(Long devId, Long communityId) {
        userCommunityService.unBindCommunity(getLoginUserId(), devId, communityId);
        return AppResult.OK;
    }

    @RequestMapping("/search/dev")
    public AppResult listDevCommunity(Long devId) {
        return AppResult.newSuccessResult(userCommunityService.listDevCommunity(devId));
    }

    @RequestMapping("/search/ad")
    public AppResult listCoummunityAd(){
        return AppResult.newSuccessResult(userCommunityService.queryAdList(getLoginUserId()));
    }
}
